package com.asset.inventory.viewModel

import androidx.lifecycle.MutableLiveData
import com.asset.inventory.base.BaseApp
import com.asset.inventory.base.BaseViewModel
import com.asset.inventory.manager.OfflineManager
import com.asset.inventory.model.ModifyAssetsModel
import com.asset.inventory.model.OfflineModel
import com.asset.inventory.model.TaskBean
import com.asset.inventory.network.HttpUtils
import com.asset.inventory.network.RequestObserver
import com.asset.inventory.network.RxScheduler
import com.asset.inventory.utils.FileUtils
import com.google.gson.Gson

/**
 *    @author : sibb
 *    date   : 21-3-16
 *    desc   : 资产盘点记录
 */
class AssetInventoryRecordViewModel: BaseViewModel() {

    private val taskList = MutableLiveData<MutableList<TaskBean.Task>>()
    private val isCommitSuccess = MutableLiveData<Boolean>()
    private val isShowLoading = MutableLiveData<Boolean>()

    fun observerCommitStatue() = isCommitSuccess

    fun observerTaskList(): MutableLiveData<MutableList<TaskBean.Task>> = taskList
    fun loadingLiveData(): MutableLiveData<Boolean> = isShowLoading

    override fun onStart() {
        super.onStart()

        if(!BaseApp.instance.isOnlineModel){
            isShowLoading.postValue(true)
            //读取SD卡任务文件
            val offlineModel = OfflineManager.readTask()
            taskList.postValue(offlineModel?.data)
            isShowLoading.postValue(false)
        }else{
            isShowLoading.postValue(true)
            getTaskList()
        }

    }

    fun commit(id: Long){
        isShowLoading.postValue(true)
        HttpUtils.instance.commit(id).compose(RxScheduler.io_main()).subscribe(object : RequestObserver<ModifyAssetsModel>{
            override fun onSuccess(t: ModifyAssetsModel) {
                if(t.status == "success"){
                    isCommitSuccess.postValue(true)
                    showToast("提交成功")
                    getTaskList()
                }else{
                    showToast("提交失败")
                    isCommitSuccess.postValue(false)
                }
                isShowLoading.postValue(false)
            }

            override fun onFaile(e: Throwable) {
                isShowLoading.postValue(false)
                showToast("提交失败")
                isCommitSuccess.postValue(false)

            }
        })
    }

    fun getTaskList(){
        HttpUtils.instance.getTaskList().compose(RxScheduler.io_main()).subscribe(object: RequestObserver<TaskBean>{
            override fun onSuccess(t: TaskBean) {
                if(t.status == "success"){
                    taskList.postValue(t.data)
                }else{
                    showToast("数据请求失败")
                }
                isShowLoading.postValue(false)

            }

            override fun onFaile(e: Throwable) {
                isShowLoading.postValue(false)
                showToast("数据请求失败")
                if (BaseApp.instance.isTest){
                    //todo 测试代码
                    testTask()
                }

            }

        })
    }

    fun testTask(){
        val testData: String = "{\n" +
                "  \"status\": \"success\",\n" +
                "  \"data\": [\n" +
                "    {\n" +
                "      \"id\": 2,\n" +
                "      \"createTime\": \"2021-04-05 01:01:27\",\n" +
                "      \"updateTime\": \"2021-04-04 23:00:00\",\n" +
                "      \"name\": \"盘点服务器\", // 盘点任务名称\n" +
                "      \"code\": \"RWA6J3SPGVC7U\",\n" +
                "      \"status\": \"doing\", // 盘点任务状态\n" +
                "      \"locations\": [\n" +
                "        \n" +
                "      ],\n" +
                "      \"locationCodes\": [\n" +
                "        \n" +
                "      ],\n" +
                "      \"assetTypes\": [\n" +
                "        \n" +
                "      ],\n" +
                "      \"assetTypeCodes\": [\n" +
                "        \"1190201010000\"\n" +
                "      ],\n" +
                "      \"orgRelation\": [\n" +
                "        \n" +
                "      ],\n" +
                "      \"orgCodes\": [\n" +
                "        \n" +
                "      ],\n" +
                "      \"orgNames\": [\n" +
                "        \n" +
                "      ],\n" +
                "      \"account\": \"yangyufei\",\n" +
                "      \"userName\": \"杨宇飞\", \n" +
                "      \"dutyResults\": [\n" +
                "        \n" +
                "      ],\n" +
                "      \"dutyResultCount\": 427, // 盘点总数\n" +
                "      \"dutyResultWaitCount\": 424, // 待盘点\n" +
                "      \"dutyResultDoneCount\": 3, // 一盘点\n" +
                "      \"inventoryPlanId\": 3,\n" +
                "      \"inventoryPlanName\": \"春季盘点计划\",\n" +
                "      \"inventoryPlanCode\": \"0004\",\n" +
                "      \"inventoryPlanCreateUser\": \"资产管理员-测试\", // 创建人\n" +
                "      \"inventoryPlanStartDate\": \"2021-04-01 00:00:00\", // 开始时间\n" +
                "      \"inventoryPlanEndDate\": \"2021-04-30 00:00:00\", // 结束时间\n" +
                "      \"inventoryPlanStatus\": \"issued\"\n" +
                "    }\n" +
                "  ]\n" +
                "}"
        val taskBean = Gson().fromJson(testData,TaskBean::class.java)
        taskList.postValue(taskBean.data)
    }
}